{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0346de4a",
   "metadata": {},
   "source": [
    "# \n",
    "\n",
    "> 1. 什么是字符串\n",
    "> 2. 字符串的一般表现形式\n",
    "> 3. 字符串的运算有哪些\n",
    "> 4. 字符串的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dea133b",
   "metadata": {},
   "source": [
    "## 1.字符串\n",
    "\n",
    "由数字、字母、下划线组成的一串字符，是编程语言中表示文本的数据类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4feef9fb",
   "metadata": {},
   "source": [
    "## 2.字符串的一般表现形式\n",
    "\n",
    "一般有单引号，双引号，三个双引号，三个单引号。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acd4f812",
   "metadata": {},
   "source": [
    "## 3. 字符串的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad80a584",
   "metadata": {},
   "source": [
    "* +连接字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c6ddba97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ddme\n"
     ]
    }
   ],
   "source": [
    "a = \"dd\"\n",
    "b = \"me\"\n",
    "c = a + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76d3fc3e",
   "metadata": {},
   "source": [
    "* 取值\n",
    "\n",
    "起始索引：结束索引：步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6b26cf23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123456\n"
     ]
    }
   ],
   "source": [
    "nfc = '123456'\n",
    "print(nfc[0:6:1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0c1729c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "654321\n"
     ]
    }
   ],
   "source": [
    "nfc = '123456'\n",
    "print(nfc[-1:-7:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78d4ce0f",
   "metadata": {},
   "source": [
    "* 切片\n",
    "\n",
    "遵循左闭右开的原则，包含起始，不包括结束"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b1fd9935",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Do\n",
      "n't panic!\n"
     ]
    }
   ],
   "source": [
    "saying = \"Don't panic!\" # 不包含结束，即不包含n\n",
    "print(saying[:2])\n",
    "print(saying[2:]) # 包含起始，即包含n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06573c70",
   "metadata": {},
   "source": [
    "* 索引的概念\n",
    "\n",
    "从左往右以0开始，从右往左从-1开始"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "234273c8",
   "metadata": {},
   "source": [
    "## 4.字符串的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f45c57a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaohong.xiaohua.xixi\n"
     ]
    }
   ],
   "source": [
    "# 1.join(字符串拼接)\n",
    "\n",
    "a = (\"xiaohong\",\"xiaohua\",\"xixi\")\n",
    "b = \".\".join(a)\n",
    "print(b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "289fd661",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# 2.in(检查字符串)\n",
    "\n",
    "a = \"小红十分聪明\"\n",
    "b = \"小红\" in a\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "57b4b045",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "# 3.find(查找元素位置)\n",
    "\n",
    "a = \"hello world\"\n",
    "b = a.find(\" \")\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4dfca9fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# 4.count(查找元素个数)\n",
    "\n",
    "a = \"我爱python，python很爱我\"\n",
    "b = a.count(\"python\")\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "75b584a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "呦吼,xiaohong\n"
     ]
    }
   ],
   "source": [
    "# 5. replace(替换字符)\n",
    "\n",
    "a = \"hi,xiaohong\"\n",
    "print(a.replace(\"hi\",\"呦吼\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "f9638871",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world']\n"
     ]
    }
   ],
   "source": [
    "# 6. split(字符串分割)\n",
    "\n",
    "a = \"hello,world\"\n",
    "print(a.split(\",\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "3445f2f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是小红，我今年21\n"
     ]
    }
   ],
   "source": [
    "# 7. format(格式化输出)\n",
    "\n",
    "age = 21\n",
    "a = \"我是小红，我今年{}\"\n",
    "print(a.format(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a206c7f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "# 8. lower(转字母为小写)\n",
    "\n",
    "a = \"Hello\"\n",
    "print(a.lower())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "97137817",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO\n"
     ]
    }
   ],
   "source": [
    "# 9. upper(转字母为大写)\n",
    "\n",
    "a = \"Hello\"\n",
    "print(a.upper())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3633ebb0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
